package com.qk.manangement.controller;

import com.qk.common.response.PageResult;
import com.qk.common.response.Result;
import com.qk.entity.pojo.Activity;
import com.qk.manangement.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/activities")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     *分页查询
     */
    @GetMapping
    public Result pageList(Integer channel, Integer type, Integer status,
                           @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize
                           ) {
        PageResult<Activity> activityPageResult = activityService.PageList(channel, type, status, page, pageSize);
        return Result.success(activityPageResult);
    }

    /**
     * 删除活动
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean delete = activityService.delete(id);
        if (delete) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 新增活动
     */
    @PostMapping
    public Result add(@RequestBody Activity activity) {
        boolean add = activityService.add(activity);
        if (add) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 根据id查询活动信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Activity activity = activityService.getById(id);
        if (activity == null) {
            return Result.error("查询失败");
        }
        return Result.success(activity);
    }

    /**
     * 修改活动信息
     */
    @PutMapping
    public Result update(@RequestBody Activity activity) {
        boolean update = activityService.update(activity);
        if (update) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

    /**
     * 查询指定类型的活动列表
     */
    @GetMapping("/type/{type}")
    public Result getListByType(@PathVariable Integer type) {
        return Result.success(activityService.getListByType(type));
    }

}
